package de.app.fbc.teams

import com.google.appengine.api.datastore.*
class VereinController {

	def persistenceManager
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
		def query = persistenceManager.newQuery( Verein )
		def  vereinInstanceList = query.execute()
		def total = 0
		if(  vereinInstanceList &&  vereinInstanceList.size() > 0){
			total =  vereinInstanceList.size()
		}
		[  vereinInstanceList :  vereinInstanceList,  vereinInstanceTotal: total ]
    }

    def show = {
	    def vereinInstance = persistenceManager.getObjectById( Verein.class, Long.parseLong( params.id )  )
        if(!vereinInstance) {
            flash.message = "Verein not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ vereinInstance : vereinInstance ] }
    }

    def delete = {
	    def vereinInstance = persistenceManager.getObjectById( Verein.class, Long.parseLong( params.id )  )
        if(vereinInstance) {
            try {
                persistenceManager.deletePersistent(vereinInstance)
                flash.message = "Verein ${params.id} deleted"
                redirect(action:list)
            }
            catch(Exception e) {
                flash.message = "Verein ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Verein not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
	    def vereinInstance = persistenceManager.getObjectById( Verein.class, Long.parseLong( params.id )  )
		if(!vereinInstance) {
            flash.message = "Verein not found with id ${params.id}"
            redirect(action:list)
        }
        else {
			vereinInstance = persistenceManager.detachCopy( vereinInstance )    
        	return [ vereinInstance : vereinInstance ]
        }
    }

    def update = {
	 	def vereinInstance = persistenceManager.getObjectById( Verein.class, Long.parseLong( params.id )  )
    
    	if(vereinInstance) {
            vereinInstance.properties = params
            if(!vereinInstance.hasErrors()){
	
				try{
					persistenceManager.makePersistent(vereinInstance)
				} catch( Exception e ){
				   	render(view:'edit',model:[vereinInstance:vereinInstance])
				}finally{
					flash.message = "Verein ${params.id} updated"
	                redirect(action:show,id:vereinInstance.id)
				}        
 			}
            else {
                render(view:'edit',model:[vereinInstance:vereinInstance])
            }
        }
        else {
            flash.message = "Verein not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def vereinInstance = new Verein()
        vereinInstance.properties = params
        return ['vereinInstance':vereinInstance]
    }

    def save = {
        def vereinInstance = new Verein(params)
		if(!vereinInstance.hasErrors() ) {
			try{
				persistenceManager.makePersistent(vereinInstance)
			} finally{
				flash.message = "Verein ${vereinInstance.id} created"
				redirect(action:show,id:vereinInstance.id)	
			}
		}
   
		render(view:'create',model:[vereinInstance:vereinInstance])
        
    }
}
